﻿CREATE PROCEDURE [maint].[CheckLastFullBackupType]
@DbName [sysname], @IsStdBackup BIT=0 OUTPUT, @IsSnapshotBackup BIT=0 OUTPUT, @OutputDiagnostics BIT=0
AS
BEGIN
	SET NOCOUNT ON

	DECLARE @RC int, @ERROR int
	SET @RC = 0

	IF(@OutputDiagnostics = 1)
		BEGIN
			PRINT OBJECT_NAME(@@PROCID)
			PRINT N'	@DbName = N''' + ISNULL(@DbName,N'NULL') + ''''
		END

	DECLARE @BackupTypeQuery nvarchar(4000)
	SET @BackupTypeQuery = N'
	SELECT @IsSnapshotBackup = COUNT(*) FROM msdb.dbo.backupset
	WHERE( [backup_set_uuid] = (
			SELECT DISTINCT [differential_base_guid]
			FROM [' + @DbName + '].sys.database_files
			WHERE ([type] = 0)
			AND ([state_desc] = ''ONLINE'')
			AND ([differential_base_guid] IS NOT NULL)
			)
		)
	AND ([is_snapshot] = 1)'

	EXEC sp_executesql @BackupTypeQuery
		,N'@IsSnapshotBackup bit OUTPUT', @IsSnapshotBackup OUTPUT

	IF(@IsSnapshotBackup = 0)
		SET @IsStdBackup = 1
		
GOTO EXIT_PROC

ERROR_HANDLER:
	IF(@RC = 0)
		SET @RC = @ERROR

EXIT_PROC:
	RETURN(@RC)
END